﻿
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows;
using System.Windows.Shapes;
namespace TimeRibbonWPF
{

    public class TimeRibbon : Canvas
    {
        public Brush RibbonBorderBrush { get; set; }

        public TimeRibbon()
        {
            RibbonBorderBrush = new SolidColorBrush(Colors.Black);
            Render();
            this.MouseMove += new MouseEventHandler(TimeRibbon_MouseMove);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(TimeRibbon_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(TimeRibbon_MouseLeftButtonUp);
        }

        private object sync = new object();
        void TimeRibbon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            lock (sync)
            {
                leftButtonDown = null;
                ReleaseMouseCapture();
            }
        }

        private Point? leftButtonDown = null;
        void TimeRibbon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            lock (sync)
            {
                Point pos = e.GetPosition(null);
                leftButtonDown = new Point(pos.X - Margin.Left, pos.Y - Margin.Top);
                CaptureMouse();
            }
        }

        void TimeRibbon_MouseMove(object sender, MouseEventArgs e)
        {
            lock (sync)
            {
                if (leftButtonDown.HasValue)
                {
                    Point currentPosition = e.GetPosition(null);
                    this.Margin = new Thickness(currentPosition.X - leftButtonDown.Value.X, currentPosition.Y - leftButtonDown.Value.Y, 0, 0);
                    Canvas.SetLeft(this, currentPosition.X - leftButtonDown.Value.X);
                    Canvas.SetTop(this, currentPosition.Y - leftButtonDown.Value.Y);
                    Render();
                }
            }
        }


        


        public double TotalSeconds
        {
            get { return (double)GetValue(TotalSecondsProperty); }
            set { SetValue(TotalSecondsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TotalSeconds.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TotalSecondsProperty =
            DependencyProperty.Register("TotalSeconds", typeof(double), typeof(TimeRibbon), 
            new PropertyMetadata(3600.0,OnTotalSecondsChanged ));
        private static void OnTotalSecondsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TimeRibbon tr = d as TimeRibbon;
            tr.Render();
        }





        public double MillisPerPixel
        {
            get { return (double)GetValue(MillisPerPixelProperty); }
            set { SetValue(MillisPerPixelProperty, value); }
        }

        public static readonly DependencyProperty MillisPerPixelProperty =
            DependencyProperty.Register("MillisPerPixelProperty", typeof(double), typeof(TimeRibbon),
            new PropertyMetadata(100.0, OnMillisChanged));

        private static void OnMillisChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TimeRibbon tr = d as TimeRibbon;
            tr.Render();
        }

        private void Render()
        {
            long secondCount = (long)TotalSeconds;
            Rectangle second = new Rectangle() { Height = Height, Width = 1000/ MillisPerPixel, Stroke = RibbonBorderBrush, StrokeThickness=1};
            Children.Clear();
            for (long i = 0; i < secondCount;i++ )
            {
                Rectangle s = new Rectangle() { Height = second.Height, Width = second.Width, Stroke = RibbonBorderBrush, StrokeThickness = 1, Fill= new SolidColorBrush(Colors.Blue) };
                Canvas.SetTop(s, 0);
                Canvas.SetLeft(s, i * s.Width);
                Children.Add(s);
            }
        }

    }
}
